home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / dr2d.lha / apps / DR2DtoPSDR2DtoPS.c < prev   
C/C++ Source or Header  |  1992-04-19  |  7KB  |  323 lines

  1. #include "iffp/obj2d.h"
  2.  
  3. /* This definition has to go somewhere so ReadDR2D can resolve it */
  4. struct Library
  5.     *IFFParseBase;
  6.  
  7. /* Forward declarations for device driver functions */
  8. void
  9.     InitDevice( struct Proj2D * ),
  10.     DisplayObject( struct Obj2D * ),
  11.     TermDevice( void );
  12.  
  13. int main( int argc, char **argv )
  14. {
  15.     struct Proj2D
  16.     *Conts;
  17.     struct Obj2D
  18.     *Obj;
  19.     int
  20.     i,
  21.     Error;
  22.     struct ParseInfo
  23.     PI;
  24.  
  25.     /* Check the argument count */
  26.     if( argc != 2 ) {
  27.     fprintf( stderr, "Usage: %s src\n", argv[0] );
  28.     fprintf( stderr, "    src is a file or -c for the clipboard\n" );
  29.     return 1;
  30.     }
  31.  
  32.     /* Open iffparse.library */
  33.     IFFParseBase = OpenLibrary( "iffparse.library", 0L );
  34.     if( !IFFParseBase ) {
  35.     fprintf( stderr, "Can't open iffparse.library\n" );
  36.     exit( 1 );
  37.     }
  38.  
  39.     /* Get an IFF handle */
  40.     PI.iff = AllocIFF();
  41.     if( !PI.iff ) {
  42.     fprintf( stderr, "Can't allocate an IFF structure\n" );
  43.     CloseLibrary( IFFParseBase );
  44.     exit( 1 );
  45.     }
  46.  
  47.     /* Open a file for reading */
  48.     Error = openifile( &PI, argv[1], IFFF_READ );
  49.     if( Error ) {
  50.     fprintf( stderr, "Can't open file %s\n", argv[1] );
  51.     CloseLibrary( IFFParseBase );
  52.     exit( 1 );
  53.     }
  54.  
  55.     Error = ReadDR2D( &PI, 0, &Conts );
  56.     closeifile( &PI );
  57.     if( Error != 0 ) {
  58.     fprintf( stderr, "Read Error: %d\n", Error );
  59.     CloseLibrary( IFFParseBase );
  60.     exit( 1 );
  61.     }
  62.  
  63.     /* Set up page, etc. */
  64.     InitDevice( Conts );
  65.  
  66.     /* Display all objects in each layer */
  67.     for( i = 0; i < Conts->NumLayer; i++ ) {
  68.     /* If this layer is not displayed, ignore it */
  69.     if( !(Conts->LayerTable[i].Flags & LAYR_DISPLAYED) )    continue;
  70.  
  71.     /* Display each object */
  72.     for( Obj = Conts->Objects[i]; Obj; Obj = Obj->Next ) {
  73.         DisplayObject( Obj );
  74.     }
  75.     }
  76.  
  77.     /* Write any epilogue stuff needed */
  78.     TermDevice();
  79.  
  80.     /* All done! Clean up. */
  81.     FreeConts( Conts );
  82.     CloseLibrary( IFFParseBase );
  83.     exit( 0 );
  84. }
  85.  
  86.  
  87. /* Forward references for display routines */
  88. void
  89.     SetAttrs( struct ATTRstruct * ),
  90.     DisplayPoly( Coord *, int, int ),
  91.     DisplaySTXT( struct Obj2D * ),
  92.     DisplayTPTH( struct Obj2D * ),
  93.     DisplayVBM( struct Obj2D * );
  94.  
  95.  
  96. /* Generic object displayer */
  97. void DisplayObject( struct Obj2D *Obj )
  98. {
  99.     switch( Obj->Type ) {
  100.     case ID_CPLY :
  101.     SetAttrs( &Obj->Attrs );
  102.     DisplayPoly(    Obj->Data.POLYdata.Coords,
  103.             Obj->Data.POLYdata.NumPoints, 1 );
  104.     break;
  105.     case ID_OPLY :
  106.     SetAttrs( &Obj->Attrs );
  107.     DisplayPoly(    Obj->Data.POLYdata.Coords,
  108.             Obj->Data.POLYdata.NumPoints, 0 );
  109.     break;
  110.     case ID_STXT :
  111.     SetAttrs( &Obj->Attrs );
  112.     DisplaySTXT( Obj );
  113.     break;
  114.     case ID_TPTH :
  115.     SetAttrs( &Obj->Attrs );
  116.     DisplayTPTH( Obj );
  117.     break;
  118.     case ID_VBM :
  119.     DisplayVBM( Obj );
  120.     break;
  121.     case ID_XTRN :
  122.     DisplayObject( Obj->Data.XTRNdata.Obj );
  123.     break;
  124.     case ID_GRUP :
  125.     for( Obj = Obj->Data.GRUPdata.Objs; Obj; Obj = Obj->Next ) {
  126.         DisplayObject( Obj );
  127.     }
  128.     break;
  129.     }
  130. }
  131.  
  132.  
  133. /*** Replace the following with your favorite device driver ***/
  134.  
  135. /* These are coded for a generic PostScript printer, with a */
  136. /* portrait page */
  137.  
  138. #define    PWIDTH        8.5
  139. #define    PHEIGHT        11.0
  140.  
  141. static struct Proj2D
  142.     *Proj;
  143. static struct ATTRstruct
  144.     CurrAttr;            /* Current attributes */
  145. static int
  146.     SwapX, SwapY,        /* Is the coordinate system reversed? */
  147.     Rot90;            /* Is the project a landscape page? */
  148.  
  149. void InitDevice( struct Proj2D *Conts )
  150. {
  151.     Coord
  152.     TX, TY;
  153.  
  154.     Proj = Conts;
  155.     SwapX = Conts->UL_X > Conts->LR_X;
  156.     SwapY = Conts->UL_Y < Conts->LR_Y;
  157.     TX = Conts->UL_X - Conts->LR_X;
  158.     TY = Conts->UL_Y - Conts->LR_Y;
  159.     if( TX < 0 )    TX = -TX;
  160.     if( TY < 0 )    TY = -TY;
  161.     Rot90 = TX > TY;
  162. }
  163.  
  164.  
  165. void TermDevice( void )
  166. {
  167.     puts( "showpage" );
  168.     /* Some PS devices need a ^D to terminate the job */
  169.     puts( "\004" );
  170. }
  171.  
  172.  
  173. void SetAttrs( struct ATTRstruct *Attr )
  174. {
  175.     CurrAttr = *Attr;
  176. }
  177.  
  178.  
  179. static void Coord2PS( Coord X, Coord Y, float *FX, float *FY )
  180. {
  181.     float
  182.     t;
  183.  
  184. #ifdef FLT_COORD
  185.     *FX = X * 72.0;
  186.     *FY = Y * 72.0;
  187. #else
  188.     *FX = (72.0*X) / (1<<FIXOFFS);
  189.     *FY = (72.0*Y) / (1<<FIXOFFS);
  190. #endif
  191.  
  192.     if( SwapX ) {
  193.     *FX = (PWIDTH*72.0) - *FX;
  194.     }
  195.     if( SwapY ) {
  196.     *FY = (PHEIGHT*72.0) - *FY;
  197.     }
  198.     if( Rot90 ) {
  199.     t = *FX; *FX = (PHEIGHT*72.0) - *FY; *FY = t;
  200.     }
  201. }
  202.  
  203.  
  204. void DisplayPoly( Coord *Points, int NumPoints, int Closed )
  205. {
  206.     int
  207.     i, n, NeedMove;
  208.     float
  209.     PX, PY;
  210.  
  211.     /* Print out the polygon */
  212.     NeedMove = 1;
  213.     for( i = 0; i < NumPoints; i++ ) {
  214.     if( ((long *)Points)[2*i] == FLT_IND ) {
  215.         if( ((long *)Points)[2*i+1] & DR2D_MOVETO ) {
  216.         /* Need a moveto... */
  217.         if( Closed ) {
  218.             /* Close the current path */
  219.             puts( "closepath" );
  220.         }
  221.         NeedMove = 1;
  222.         }
  223.         if( ((long *)Points)[2*i+1] & DR2D_SPLINE ) {
  224.         /* Do a curveto */
  225.         Coord2PS( Points[2*i+2], Points[2*i+3], &PX, &PY );
  226.         if( NeedMove ) {
  227.             printf( "%g %g moveto\n", PX, PY );
  228.         }
  229.         else {
  230.             printf( "%g %g lineto\n", PX, PY );
  231.         }
  232.         Coord2PS( Points[2*i+4], Points[2*i+5], &PX, &PY );
  233.         printf( "%g %g ", PX, PY );
  234.         Coord2PS( Points[2*i+6], Points[2*i+7], &PX, &PY );
  235.         printf( "%g %g ", PX, PY );
  236.         Coord2PS( Points[2*i+8], Points[2*i+9], &PX, &PY );
  237.         printf( "%g %g curveto\n", PX, PY );
  238.         NeedMove = 0;
  239.         i += 4;    /* Skip the curveto points */
  240.         }
  241.     }
  242.     else {
  243.         Coord2PS( Points[2*i], Points[2*i+1], &PX, &PY );
  244.         if( NeedMove ) {
  245.         printf( "%g %g moveto\n", PX, PY );
  246.         }
  247.         else {
  248.         printf( "%g %g lineto\n", PX, PY );
  249.         }
  250.         NeedMove = 0;
  251.     }
  252.     }
  253.  
  254.     if( Closed ) {
  255.     puts( "closepath" );
  256.     }
  257.  
  258.     if( Closed && (CurrAttr.FillType == FT_COLOR) ) {
  259.     /* Fill the polygon */
  260.     if( CurrAttr.EdgePattern ) {
  261.         /* Save the path for the later stroke */
  262.         puts( "gsave" );
  263.     }
  264.     n = CurrAttr.FillValue;
  265.     printf( "%g %g %g setrgbcolor eofill\n",
  266.             Proj->RGB_Table[3*n] / 255.0,
  267.             Proj->RGB_Table[3*n+1] / 255.0,
  268.             Proj->RGB_Table[3*n+2] / 255.0 );
  269.     if( CurrAttr.EdgePattern ) {
  270.         /* Restore the path */
  271.         puts( "grestore" );
  272.     }
  273.     }
  274.  
  275.     if( CurrAttr.EdgePattern ) {
  276.     /* Stroke the polygon */
  277.     /* First, establish the dash pattern */
  278.     n = CurrAttr.EdgePattern;
  279.     NumPoints = Proj->DashCounts[n];
  280.     putchar( '[' );
  281. #ifdef FLT_COORD
  282.     PX = CurrAttr.EdgeThick * 72.0;
  283. #else
  284.     PX = (72.0*CurrAttr.EdgeThick) / (1<<FIXOFFS);
  285. #endif
  286.     printf( "%g setlinewidth\n", PX );
  287. #ifndef FLT_COORD
  288.     PX /= (1<<FIXOFFS);
  289. #endif
  290.     for( i = 0; i < NumPoints; i++ ) {
  291.         printf( " %g", Proj->DashPatts[n][i] * PX );
  292.     }
  293.     puts( " ] 0 setdash" );
  294.  
  295.     /* Now, establish the line join */
  296.     printf( "%d setlinejoin\n", CurrAttr.JoinType );
  297.  
  298.     /* Finally, set the color and stroke the line */
  299.     n = CurrAttr.EdgeValue;
  300.     printf( "%g %g %g setrgbcolor stroke\n",
  301.             Proj->RGB_Table[3*n] / 255.0,
  302.             Proj->RGB_Table[3*n+1] / 255.0,
  303.             Proj->RGB_Table[3*n+2] / 255.0 );
  304.     }
  305. }
  306.  
  307. void DisplaySTXT( struct Obj2D *Obj )
  308. {
  309.     /* TBD someday */
  310. }
  311.  
  312. void DisplayTPTH( struct Obj2D *Obj )
  313. {
  314.     /* TBD someday */
  315. }
  316.  
  317. void DisplayVBM( struct Obj2D *Obj )
  318. {
  319.     /* TBD someday */
  320. }
  321.  
  322. /*** EOF DR2DtoPS.c ***/
  323.